﻿using System;
using System.Linq;
using System.Reflection;
using Core.Domain.Interfaces;
using FluentNHibernate;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace DataMain.PersistanceRepositories
{
  public class NHibernateRepository: IPersistanceRepository
  {
    public T Get<T>(int id)
    {
      throw new NotImplementedException();
    }

    public void Save<T>(T entity)
    {
      throw new NotImplementedException();
    }

    public IQueryable<T> Query<T>()
    {
      throw new NotImplementedException();
    }
  }

  /*public class NHibernateRepository : IPersistanceRepository
  {
     private readonly ISessionLocator sessionLocator;

     public NHibernateRepository(ISessionLocator sessionLocator)
     {
       this.sessionLocator = sessionLocator;
     }

     #region IPersistanceRepository Members

     public T Get<T>(int id)
     {
       ISession session = sessionLocator.ActiveSession();

       return session.Get(id);
     }

     public void Save<T>(T entity)
     {
     }

     public IQueryable<T> Query<T>()
     {
       var qry = from t in sessionLocator.ActiveSession().Linq()
                 select t;

       return qry.AsQueryable();
     }

     #endregion
   }

   internal class NHiberanteSessionLocator
   {
     private static readonly ISessionFactory sessionFactory;
     private ISession currentSession;

     static NHiberanteSessionLocator()
     {
       sessionFactory = GetConfiguration().BuildSessionFactory();
     }

     private static Configuration GetConfiguration()
     {
       var cfg = new Configuration().Configure();

       string mappingAssembly = ConfigurationManager
             .AppSettings["NHibernate.Mapping.Assembly"];

       var persistenceModel = new PersistenceModel();

       persistenceModel
             .addMappingsFromAssembly(Assembly.Load(mappingAssembly));
       persistenceModel.Configure(cfg);

       return cfg;
     }

     public ISession ActiveSession()
     {
       if (currentSession == null)
       {
         currentSession = sessionFactory.OpenSession();
       }

       return currentSession;
     }

     public void CloseActiveSession()
     {
     }

     public void UpdateDatabaseSchema()
     {
       var cfg = GetConfiguration();

       var schemaUpdate = new SchemaUpdate(cfg);

       schemaUpdate.Execute(true, false);
  }   }*/
  
}